package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.reservation.CountByTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 预约信息Service业务层处理
 *
 * @author fan
 * @date 2025-06-06
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation) {
        reservation.setStatus(0);
        //获取当前登录人
        Long userId = UserThreadLocal.getUserId();
        reservation.setCreateBy(String.valueOf(userId));
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询当天取消预约数量
     */
    @Override
    public int getCancelledCount() {
        //获取当前登录人
        Long userId = UserThreadLocal.getUserId();

        //获取当天开始时间和结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        //查询当天取消预约数量
        LambdaQueryWrapper<Reservation> reservationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        reservationLambdaQueryWrapper.eq(Reservation::getStatus, 2)
                .eq(Reservation::getCreateBy, userId)
                .between(Reservation::getCreateTime, startTime, endTime);

        return (int) count(reservationLambdaQueryWrapper);

    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time 时间戳
     */
    @Override
    public List<CountByTimeVo> countByTime(Long time) {
        //获取当天开始时间和结束时间
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        List<CountByTimeVo> countByTimeVos = reservationMapper.selcetCountByTime(ldt, endTime);

        return countByTimeVos;
    }

    /**
     * 分页查询预约
     */
    @Override
    public List<Reservation> pageList(Integer pageNum, Integer pageSize, Integer status) {
        //获取当前登录人
        Long userId = UserThreadLocal.getUserId();

        LambdaQueryWrapper<Reservation> reservationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        reservationLambdaQueryWrapper.eq(Reservation::getCreateBy, userId);
        if (status != null) {
            reservationLambdaQueryWrapper.eq(Reservation::getStatus, status);
        }

        List<Reservation> reservations = list(reservationLambdaQueryWrapper);
        return reservations;

    }

    /**
     * 取消预约
     */
    @Override
    public int cancelReservation(Long id) {
        return reservationMapper.cancelReservation(id);
    }

    /**
     * 自动更新预约状态
     */
    @Override
    public void updateReservationStatus() {
        reservationMapper.updateReservationStatus();

    }
}
